package me.landery.modules.sinco.service.impl;

import lombok.RequiredArgsConstructor;
import me.landery.modules.sinco.domain.Mode;
import me.landery.modules.sinco.domain.Vip;
import me.landery.modules.sinco.repository.VipRepository;
import me.landery.modules.sinco.service.ModeService;
import me.landery.modules.sinco.service.VipService;
import me.landery.modules.sinco.service.dto.VipDto;
import me.landery.modules.sinco.service.dto.VipQueryCriteria;
import me.landery.modules.sinco.service.mapstruct.VipMapper;
import me.landery.modules.system.service.UserService;
import me.landery.modules.system.service.dto.UserDto;
import me.landery.modules.system.service.mapstruct.UserMapper;
import me.landery.utils.PageUtil;
import me.landery.utils.QueryHelp;
import me.landery.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;
import java.util.Map;

/**
 * @Author cc.w
 * @Date 2022/6/7 16:55
 * @Version 1.0
 */
@Service
@RequiredArgsConstructor
@CacheConfig(cacheNames = "vip")
public class VipServiceImpl implements VipService {

    private final VipRepository vipRepository;
    private final VipMapper vipMapper;

    private final UserService userService;
    @Lazy
    @Autowired
    private ModeService modeService;
    private final UserMapper userMapper;

    @Override
    public Map<String, Object> queryAll(VipQueryCriteria criteria, Pageable pageable) {
        Page<Vip> page = vipRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(vipMapper::toDto).getContent(),page.getTotalElements());
    }

    @Override
    public VipDto findByUserID(Long userId) {
        Vip vip = vipRepository.findByUserId(userId);
        return vipMapper.toDto(vip);
    }

    @Override
    public Vip save(Vip vip) {
        return vipRepository.save(vip);
    }

    @Override
    @Transactional
    public void checkUserVip() {
        UserDto userDto = userService.findById(SecurityUtils.getCurrentUserId());
        //得到当前用户的vip
        Vip vip = vipRepository.findByUserId(userDto.getId());
        //获取按次付费的套餐
        Mode mode = modeService.findById(0L);

        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE,mode.getDay());
        Date endDate = calendar.getTime();
        //没有vip状态，新增一条，不走订购,默认套餐0
        if (vip == null){
            vip = new Vip();
            vip.setPrice(mode.getTax());
            vip.setQuota(mode.getQuota());
            vip.setMode(mode);
            vip.setBalance(0.0);
            vip.setUser(userMapper.toEntity(userDto));
            vip.setExpiration(endDate);
            vip.setProfit(0.0);
            vipRepository.save(vip);
            return;
        }

        // 套餐已过期
        if (vip.getExpiration().before(date)){
            //重置付费价格，免费额度，套餐类型
            vip.setQuota(0L);
            vip.setPrice(mode.getTax());
            vip.setMode(mode);
            vip.setExpiration(calendar.getTime());
            vipRepository.save(vip);
        }
    }

    @Override
    public void updateProfit(Long currentUserId, double v) {
        Vip vip = vipRepository.findByUserId(currentUserId);
        vip.setProfit(vip.getProfit() + v);
        vipRepository.save(vip);
    }


}
